package com.n9.qq.android.login;

import com.n9.qq.android.Const;
import com.n9.qq.android.login.tlv.Tlv0002;
import com.n9.qq.android.login.tlv.Tlv0008;
import com.n9.qq.android.login.tlv.Tlv0104;
import com.n9.qq.android.login.tlv.Tlv0116;
import com.n9.qq.util.Crypter;

import java.nio.ByteBuffer;

/**
 * @author ice
 * @since 2019/2/19 14:19
 */
public class ReqCommitVFCode extends BaseReqLoginBody{

    int cmd = 0x02;
    private String vfCode;
    private byte[] imgIdBytes;
    private byte[] imgToken;

    public ReqCommitVFCode(long qqUin, String vfCode, byte[] imgIdBytes, byte[] imgToken) {
        this.qqUin = qqUin;
        this.vfCode = vfCode;
        this.imgIdBytes = imgIdBytes;
        this.imgToken = imgToken;
    }

    @Override
    protected byte[] encryptData(byte[] shareKey) {
        Tlv0002 tlv0002 = new Tlv0002();
        byte[] ds0002Data = tlv0002.getData(vfCode.getBytes(), imgIdBytes);
        Tlv0008 tlv0008 = new Tlv0008();
        byte[] ds0008Data = tlv0008.getData(0, 0x00000804, 0);
        Tlv0104 tlv0104 = new Tlv0104();
        byte[] ds0104Data = tlv0104.getData(imgToken);
        Tlv0116 tlv0116 = new Tlv0116();
        byte[] ds0116Data = tlv0116.getData(Const.M_MISC_BIT_MAP, Const.M_SUB_SIG_MAP, null);

        int len = ds0002Data.length + ds0008Data.length + ds0104Data.length + ds0116Data.length + 4;
        ByteBuffer buf = ByteBuffer.allocate(len);
        buf.putShort((short)cmd);
        buf.putShort((short)4);
        buf.put(ds0002Data);
        buf.put(ds0008Data);
        buf.put(ds0104Data);
        buf.put(ds0116Data);

        return new Crypter().encrypt(buf.array(), shareKey);
    }
}
